package top.lemna.account.domain;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import top.lemna.account.persistence.YesNo;
import top.lemna.common.Amount;

@Builder(access = AccessLevel.PACKAGE)
@Getter
// @Setter(value=AccessLevel.PACKAGE)
@ToString
public class Account { 

    /** 账户Id */
    private Long id;

    /** 账户名称 */
    private String name;

    /** 账户余额  */
    private Amount balance = new Amount();

    /** 冻结金额 可以大于账户余额 */
    private Amount freeze= new Amount();

    /** 是否锁定 */
    private YesNo block = YesNo.NO;
     
    public void handle(EventQueue queue, CreditEnterAccountCommand command) {
        if (!balance.ge(command.getAmount())) {
            queue.enqueue(new CreditEnterAccountFailedEvent());
            return;
        }
        balance.subtractFrom(command.getAmount());
        queue.enqueue(new CreditEnterAccountEvent());
    }

}
